package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import server.NewsServer;
import serverImp.NewsServerImp;
import entity.News;

public class NewsAddServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out= response.getWriter();
		String title=request.getParameter("title");
		String author=request.getParameter("author");
		String content=request.getParameter("content");
		News news=new News(title, author, content);
		NewsServer ns=new NewsServerImp();
		if(ns.add(news)>0){
			out.print("<script type='text/javascript'>alert('添加成功');</script>");
			response.sendRedirect("ListServlet");
		}else{
			out.print("<script type='text/javascript'>alert('添加失败');</script>");
			response.sendRedirect("ListServlet");
		}
	}

}
